/*
 * $Id: SysCodeEdit.java 17 2014-08-28 10:52:50Z ratking $
 *
 * Copyleft (C) 2014 RatKing. All wrongs reserved.
 */
package cn.ratking.demo.jaws6.system.view;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.inject.Named;

import org.omnifaces.util.Messages;
import org.slf4j.Logger;

import cn.ratking.demo.jaws6.model.LogEvent;
import cn.ratking.demo.jaws6.model.SysCode;
import cn.ratking.demo.jaws6.system.service.SysCodeService;

/**
 * 系统代码查询
 */
@Named
@ConversationScoped
public class SysCodeEdit implements Serializable {

    /** 系统日志 */
    @Inject
    private transient Logger log;
    @Inject
    private SysCodeService sysCodeService;
    @Inject
    private Conversation conversation;
    /** 事件源 */
    @Inject
    private Event<LogEvent> logEventSrc;
    /** 系统代码ID */
    private Long sysCodeId;
    /** 选择的系统代码 */
    private SysCode sysCode;

    @PostConstruct
    public void init() {
        //log.debug("Debug: SysCodeEdit.init(), this=" + this);
    }

    @PreDestroy
    public void destroy() {
        //log.debug("Debug: SysCodeEdit.destroy(), this=" + this);
    }

    /**
     * 开始业务会话
     */
    public void beginConversation() {
        if (conversation.isTransient()) {
            conversation.begin();
        }
        conversation.setTimeout(300000); //5 * 60 * 1000 (5 minutes)
    }

    /**
     * 结束业务会话
     */
    public void endConversation() {
        if (!conversation.isTransient()) {
            conversation.end();
        }
    }

    public Long getSysCodeId() {
        return sysCodeId;
    }

    public void setSysCodeId(Long sysCodeId) {
        this.sysCodeId = sysCodeId;
    }

    public boolean isManaged() {
        return sysCodeId != null;
    }

    public SysCode getSysCode() {
        if (sysCode == null) {
            if (isManaged()) {
                sysCode = sysCodeService.findById(sysCodeId);
            } else {
                sysCode = new SysCode();
                sysCode.setEnabled(true);
                sysCode.setOrderNo(1);
                //sysCode.setVersion(0);
            }
        }
        return sysCode;
    }

    public void setSysCode(SysCode sysCode) {
        this.sysCode = sysCode;
    }

    /**
     * 添加/修改系统代码
     *
     * @return 导航页面
     */
    public String save() {
        try {
            if (isManaged()) {
                sysCodeService.update(sysCode);
            } else {
                sysCodeService.create(sysCode);
            }
            // 结束业务会话
            endConversation();

            String msg = (isManaged() ? "修改" : "添加") + "系统代码["
                    + sysCode.getCodeType() + "-" + sysCode.getCodeName()
                    + "]成功";
            log.debug(msg);
            logEventSrc.fire(new LogEvent(LogEvent.LOGTYPE_APPLICATION, LogEvent.LOGLEVEL_INFO, msg));
            Messages.addGlobalInfo(msg);
        } catch (Exception ex) {
            String errorMsg = (isManaged() ? "修改" : "添加") + "系统代码["
                    + sysCode.getCodeType() + "-" + sysCode.getCodeName()
                    + "]异常：" + ex.getMessage();
            log.error(errorMsg, ex);
            logEventSrc.fire(new LogEvent(LogEvent.LOGTYPE_APPLICATION, LogEvent.LOGLEVEL_ERROR, errorMsg));
            Messages.addGlobalError(errorMsg);
            return null;
        }
        return "sysCodeQuery?faces-redirect=true";
    }

    /**
     * 返回系统代码查询列表
     *
     * @return 导航页面
     */
    public String cancel() {
        if (isManaged()) {
            sysCodeService.detach(sysCode);
        }
        // 结束业务会话
        endConversation();
        return "sysCodeQuery?faces-redirect=true";
    }
}
